//
//  LBChatViewController.m
//  IMDemo
//
//  Created by 刘文扬 on 16/8/11.
//  Copyright © 2016年 lianj. All rights reserved.
//

#import "LBChatViewController.h"
#import <UITextView+Placeholder.h>
#import "LBDiscussBidMessage.h"
#import "LBDiscussBidMessageCell.h"
#import "LBRCIMManager.h"
#import <IQKeyboardManager.h>
#import "UIView+LBFindViewController.h"
#define kmessagePerPageCout 10
#define maxImageLength 940

@interface LBChatViewController ()<UIGestureRecognizerDelegate>

/** <#注释#>*/
@property(nonatomic,strong)RCMessageModel *longSelectionModel;

@end

@implementation LBChatViewController
-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
- (BOOL)prefersStatusBarHidden{
    return NO;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

{
    
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
    
}

- (BOOL)shouldAutorotate

{
    
    return YES;
    
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{
    return UIInterfaceOrientationMaskPortrait;//只支持这一个方向(正常的方向)
}


-(id)initWithConversationType:(RCConversationType)conversationType targetId:(NSString *)targetId
{
    self = [super initWithConversationType:conversationType targetId:targetId];
    if (self) {
        
        self.displayUserNameInCell = conversationType == ConversationType_PRIVATE ? NO : YES;
//        
//        if ([[RCIM sharedRCIM] getConnectionStatus] == ConnectionStatus_Unconnected) {
//            
//            [LBRCIMManager reConectRongYun];
//        }
    }
    return self;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    self.chatSessionInputBarControl.inputTextView.placeholder = @"请输入...";
    [self.chatSessionInputBarControl.pluginBoardView removeItemAtIndex:2];
    [self.chatSessionInputBarControl.pluginBoardView removeItemAtIndex:2];
    [self.chatSessionInputBarControl.pluginBoardView removeItemAtIndex:2];
    [self registerClass:[LBDiscussBidMessageCell class] forMessageClass:[LBDiscussBidMessage class]];
    if (self.type == LBChatViewControllerTypePop) {
        
        self.view.backgroundColor = [UIColor clearColor];
        CGRect collectionViewRect = self.conversationMessageCollectionView.frame;
        collectionViewRect.origin.y = 120;
        self.conversationMessageCollectionView.frame = collectionViewRect;
    }

    
    self.chatSessionInputBarControl.emojiBoardView.backgroundColor = [UIColor whiteColor];
    
    
    if (self.parentViewController != nil) {
        //修复融云内嵌之后不监听键盘的bug
        [[NSNotificationCenter defaultCenter] addObserver:self.chatSessionInputBarControl selector:@selector(rcInputBar_didReceiveKeyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
        
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss:)];
        gesture.delegate = self;
        [self.conversationMessageCollectionView addGestureRecognizer:gesture];
    }
    
}

-(void)dismiss:(UITapGestureRecognizer *)gesture
{
    [self.view endEditing:YES];
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}


-(void)didLongTouchMessageCell:(RCMessageModel *)model inView:(UIView *)view
{

    self.longSelectionModel = model;
    [super didLongTouchMessageCell:model inView:view];

}



-(NSArray<UIMenuItem *> *)getLongTouchMessageCellMenuList:(RCMessageModel *)model
{
    UIMenuItem *copyItem = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(onCopyMessage:)];
    if ([model.content isMemberOfClass:[RCTextMessage class]]) {
    
        return @[copyItem];
    }
    
    return nil;
}

-(void)onCopyMessage:(UIMenuItem *)copyItem
{
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    RCTextMessage *textMessage = (RCTextMessage *)self.longSelectionModel.content;
    pasteboard.string = textMessage.content;
}

-(BOOL)canBecomeFirstResponder{
    
    return YES;
}



-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enable = NO;
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enable = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(void)dealloc
{

    NSLog(@"聊天VC销毁了");
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
